import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

class Solution {
    public int minOperations(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        int n = nums.length;
        for (int x : nums) {
            set.add(x);
        }
        List<Integer> arr = new ArrayList<>(set);
        Collections.sort(arr);
        int ret = n;
        int j = 0;
        for (int i = 0; i < arr.size(); i++) {
            int left = arr.get(i);
            int right = left + n - 1;
            while (j < arr.size() && arr.get(j) <= right) {
                ret = Math.min(ret, n - (j - i + 1));
                j++;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{1, 3, 7, 8, 9};
        System.out.println(solution.minOperations(nums));
    }
}